
package de.swta.freezay.database.wicketAdapter;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.dbEntities.Category;
import org.apache.wicket.model.LoadableDetachableModel;

/**
 * A LoadableDetachableModel for category entities
 *
 * @author Christian Baeumer
 * @see LoadableDetachableModel
 */
public class DetachableCategoryModel extends LoadableDetachableModel<Category> {

    /* the category id for this category model */
    private int categoryId;

    public DetachableCategoryModel(final Category c) {
        this(c.getId());
    }

    public DetachableCategoryModel(final int id) {
        categoryId = id;
    }

    @Override
    protected Category load() {
        /** loads Category from database */
        return JPAController.CategoryJpaController.findCategory(categoryId);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 47 * hash + categoryId;
        return hash;
    }

    /**
     * used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
     *
     * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else if (obj instanceof DetachableCategoryModel) {
            DetachableCategoryModel other = (DetachableCategoryModel) obj;
            return other.categoryId == categoryId;
        }

        return false;
    }

}
